package com.jzsm.common.utils;

import com.jzsm.common.exception.BusinessException;

import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;

public class AssertUtils {
    /**
     * 字符 不能为空
     *
     * @param msg 错误消息
     * @param str 字符
     */
    public static void isNotBlank(String msg, String str) {
        if (StringUtils.isBlank(str)) {
            throw new BusinessException(msg);
        }
    }
    /**
     * 字符为空
     *
     * @param msg 错误消息
     * @param str 字符
     */
    public static void isBlank(String msg, String str) {
        if (StringUtils.isNotBlank(str)) {
            throw new BusinessException(msg);
        }
    }

    /**
     * 不能为 null
     *
     * @param msg 错误消息
     * @param obj 参数
     */
    public static <T> void isNotNull(String msg, T obj) {
        if (Objects.isNull(obj)) {
            throw new BusinessException(msg);
        }
    }

    /**
     * 集合不能为空
     *
     * @param msg        错误消息
     * @param collection 集合
     */
    public static <T> void isNotEmpty(String msg, Collection<T> collection) {
        if (Objects.isNull(collection) || collection.isEmpty()) {
            throw new BusinessException(msg);
        }
    }

    /**
     * 必须为真
     *
     * @param msg  错误消息
     * @param bool boolean值
     */
    public static void isTrue(String msg, boolean bool) {
        if (!bool) {
            throw new BusinessException(msg);
        }
    }

    /**
     * 不能满足条件
     *
     * @param msg  错误消息
     * @param bool val
     */
    public static void isFalse(String msg, boolean bool) {
        if (bool) {
            throw new BusinessException(msg);
        }
    }

    /**
     * 必须 包含
     *
     * @param msg   错误消息
     * @param elem  值
     * @param elems 集合
     */
    public static <T> void isIn(String msg, T elem, Collection<T> elems) {
        isNotNull(msg, elems);
        if (!elems.contains(elem)) {
            throw new BusinessException(msg);
        }
    }

    /**
     * 必须在其中
     *
     * @param msg   错误消息
     * @param ele   值
     * @param elems 集合
     */
    @SafeVarargs
    public static <T> void isIn(String msg, T ele, T... elems) {
        long count = Arrays.stream(elems).filter(item -> Objects.equals(ele, item)).count();
        if (Objects.equals(count, 0)) {
            throw new BusinessException(msg);
        }
    }

    /**
     * 测试跑错
     */
    public static void test(){
        throwError("test-flag");
    }
    public static void throwError(String msg){
        throw new BusinessException(msg);
    }
}
